package org.niu.str;

import java.util.*;

public class ToKStr {
    public String[][] topKstrings (String[] strings, int k) {
        // write code here
        String[][] ans = new String[11][1000001];
        PriorityQueue<Map.Entry<String,Integer>> q = new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if(!o1.getValue().equals(o2.getValue())){
                    return o2.getValue()-o1.getValue();
                }
                return o1.getKey().compareTo(o2.getKey());
            }
        });
        Map<String,Integer> mp = new HashMap<>();
        for (String string : strings) {
            mp.put(string,mp.getOrDefault(string,0)+1);
        }
        for(Map.Entry<String,Integer> entry:mp.entrySet()) {
            q.add(new AbstractMap.SimpleEntry<>(entry.getKey(), entry.getValue()));
        }
        int i = 0;
        while(k!=0){
            Map.Entry<String, Integer> poll = q.poll();
            String[] t = new String[2];
            t[0] = poll.getKey();
            t[1] = poll.getValue().toString();
            ans[i] = t;
            i++;
            k--;
        }
        return ans;
     }
}
